package cn.yunyichina.provider.coupon;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class ScanCouponCode {

    public static void main(String[] args) {

        File dir = new File("C:\\Temp\\");
        List<String> codes = new ArrayList<String>();

        int handleCount = 0;
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.exists()) {
                    try {
                        // 读出所有的订单号
                        BufferedReader reader = null;
                        try {
                            reader = new BufferedReader(new FileReader(file));
                            String tempString = null;
                            int line = 1;
                            while ((tempString = reader.readLine()) != null) {
                                codes.add(tempString.trim());
                                line++;
                            }
                            reader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (reader != null) {
                                try {
                                    reader.close();
                                } catch (IOException e1) {
                                }
                            }
                        }
                        System.out.println("扫描券码个数为：" + codes.size());

                        FileOutputStream out;
                        try {
                            out = new FileOutputStream(
                                    "C:\\TempBak\\" + System.currentTimeMillis() + "-" + file.getName());
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                            return;
                        }
                        
                        PrintStream p = new PrintStream(out);
                        
                        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String strDate = dateformat.format(new Date());

                        for (String code : codes) {
                            StringBuffer sb = new StringBuffer();
                            sb.append(
                                    "insert into cashpool_coupon_code(ID,COUPON_ID,COUPON_CODE,IS_PUSHED,IS_RECEIVED,PUSH_TIME,CREATE_TIME,RECEIVE_TIME,OPEN_ID) VALUES(\"")
                                    .append(UUID.randomUUID().toString().replaceAll("-", "")).append("\",\"")
                                    .append(file.getName().replace(".txt", "")).append("\",\"").append(code)
                                    .append("\",0,0,null,\"").append(strDate)
                                    .append("\",null,null").append(");");

                            System.out.println(sb.toString());

                        }
                        
                        file.delete();

                        try {
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        p.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("扫描报错，已处理行数：" + handleCount);
                    }
                }
            }
        }

    }

}
